package com.example.controller;

import com.example.pojo.Browse;
import com.example.pojo.Result;
import com.example.service.impl.BrowseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@CrossOrigin
@RequestMapping("/browse")
@RestController
public class BrowseController {

    @Autowired
    private BrowseServiceImpl browseService;

    @Autowired
    private HttpServletRequest request;

    //添加浏览记录
    @PostMapping
    public Result addBrowse(@RequestBody Browse browse){
        browseService.addBrowse(browse,request);
        return Result.success();
    }

    //查看浏览记录
    @GetMapping
    public Result getBrowse(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime date){
        List<Browse> list=browseService.getBrowse(date,request);
        return Result.success(list);
    }

    //删除浏览记录
    @DeleteMapping("/{id}")
    public Result delBrowse(@PathVariable Integer id){
        browseService.delBrowse(id,request);
        return Result.success();
    }
}
